package com.fruit.controller.app.user;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.user.SysUser;
import com.fruit.pojo.user.SysWapMenu;
import com.fruit.service.user.SysRoleManager;
import com.fruit.service.user.SysUserManager;
import com.fruit.util.Const;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;




@Controller
@RequestMapping(value = "/app/user")
public class UserController extends BaseController {

	@Autowired
	private SysRoleManager sysRoleService;
	
	@Autowired
	private SysUserManager sysUserService;
	
	@Value("${IMAGE_BASE_URL}")
	private String IMAGE_BASE_URL;
	
	@RequestMapping
	@ResponseBody
	public Map<String, Object> init(String userId){
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{	
			SysUser sysUser = new SysUser();
			sysUser.setUserId(userId);
			sysUser = sysUserService.getUserInfo(sysUser);
			List<SysWapMenu> wapMenu = sysRoleService.getUserWapMenu(sysUser.getRoleId());				
				
			if(wapMenu != null && wapMenu.size() > 0){
				List<SysUser> transaction=null;
				List<SysUser> order=null;
				//设置权限
				sysUser.setIsAllView(sysRoleService.getQxNameCnt(sysUser.getRoleId(),"all_view")?"1":"0");
				transaction=sysUserService.getTransactionNum(sysUser);
				order=sysUserService.getOrderNum(sysUser);
				
				
				Map<String, String> transMap = new HashMap<String, String>();
				if(transaction!= null && transaction.size()>0){
					for (SysUser sys : transaction) {
						transMap.put(sys.getState(), sys.getNumber());
					}
				}
				
				Map<String, String> orderMap = new HashMap<String, String>();
				if(order!= null && order.size()>0){
					for (SysUser sys : order) {
						orderMap.put(sys.getState(), sys.getNumber());
					}
				}
				for(SysWapMenu swm:wapMenu){
					swm.setMenuIcon(IMAGE_BASE_URL+swm.getMenuIcon());
					
					if("07".equals(swm.getMenuCode())){
						if(transMap.containsKey("00")){
							swm.setHandelNum(transMap.get("00"));
						}
					}
					if("06".equals(swm.getMenuCode())){
						if(orderMap.containsKey("06")){
							swm.setHandelNum(orderMap.get("06"));
						}
					}
					if("05".equals(swm.getMenuCode())){
						if(orderMap.containsKey("05")){
							swm.setHandelNum(orderMap.get("05"));
						}
					}
					if("04".equals(swm.getMenuCode())){
						if(orderMap.containsKey("04")){
							swm.setHandelNum(orderMap.get("04"));
						}
					}
					if("03".equals(swm.getMenuCode())){
						if(orderMap.containsKey("03")){
							swm.setHandelNum(orderMap.get("03"));
						}
					}
					if("02".equals(swm.getMenuCode())){
						if(orderMap.containsKey("02")){
							swm.setHandelNum(orderMap.get("02"));
						}
					}
					if("01".equals(swm.getMenuCode())){
						if(orderMap.containsKey("00")){
							swm.setHandelNum(orderMap.get("00"));
						}
					}
				}
			}
			resMap.put("wapMenu", wapMenu);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", "成功"));
			return resMap;
		}catch(Exception e){
			e.printStackTrace();
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常!"));
			return resMap;
		}
		
	}
	
	@RequestMapping(value = "getUserToken", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getUserToken(HttpServletRequest request,HttpServletResponse response, HttpSession session, String userId){
		this.logBefore(logger, "获取UserToken");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
		SysUser sysUser = new SysUser();
		sysUser.setUserId(userId);
		sysUser = sysUserService.getUserInfo(sysUser);
		session.removeAttribute("sysUser");
		session.setAttribute("sysUser", sysUser);
		resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", "成功"));
		resMap.put("sessionId", session.getId());
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统繁忙!"));
		}
		this.logAfter(logger);
		return resMap;
	}		
	
}
